﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using QLQTPM_ThuDuc_Group7.DTO;
using System.Windows.Forms;
using DesignLayOut_QLQTPM_Team7.DAO;

namespace QLQTPM_ThuDuc_Group7.DAO
{
    public class DichVuDAO
    {
        public List<DichVuDTO> LayDanhSachDichVu()
        {
            List<DichVuDTO> lstDichVu = new List<DichVuDTO>();
            DataProvider dp = new DataProvider();
            string sql = "select *from dichvu";
            DataTable dt = new DataTable();
            dt = dp.executeQuery(sql);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DichVuDTO dichvu = new DichVuDTO();
                dichvu.MaDichVu = Convert.ToInt32(dt.Rows[i]["madichvu"]);
                dichvu.TenDichVu = dt.Rows[i]["tendichvu"].ToString();
                lstDichVu.Add(dichvu);
            }
            return lstDichVu;
        }
        //-----Bat dau Insert------
        DataProvider dataProvider = new DataProvider();

        public bool ThemDichVu(DichVuDTO dvDTO)
        {
            bool flag = false;
            string err = "";
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            dataProvider.connect();
            try
            {
                // B3: Tao chuoi strSQL thao tac CSDL
                bool kt = KiemTraLoaiDichVuCoTonTaiKhong(dvDTO.TenDichVu);
                try
                {
                    if (kt == true)
                    {
                        err += "Loại hình dịch vụ này đã có trong cơ sở dữ liệu";
                        MessageBox.Show(err, "Lỗi !!!");
                    }
                    else
                    {
                        string strSQL = "insert into dichvu (TenDichVu )values ('" + dvDTO.TenDichVu + "')";
                        bool co = dataProvider.executeNonQuery(strSQL);
                        if (co == true)
                        {
                            flag = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return flag;
        }

        public bool KiemTraLoaiDichVuCoTonTaiKhong(string tenDichVu)
        {
            bool flag = false;
            OleDbConnection cn;
            try
            {
                cn = DataProvider.ConnectionData();
                string strSQL1 = "select * from dichvu where TenDichVu = '" + tenDichVu + "'";
                object kt1 = dataProvider.executeScalar(strSQL1);

                string strSQL2 = "select * from tukhoadichvu where TuKhoaDichVu = '" + tenDichVu + "'";
                object kt2 = dataProvider.executeScalar(strSQL2);
                if (kt1 != null || kt2 != null)
                {
                    flag = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Loại dịch vụ này đã có " + ex.Message);
            }
            return flag;
        }

        public DataTable LayDanhSachCacLoaiDichVu()
        {
            DataTable dt = new DataTable();
            OleDbConnection cn;
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            cn = DataProvider.ConnectionData();
            // B3: Tao chuoi strSQL thao tac CSDL
            string strSQL;
            strSQL = "Select * From dichvu";
            OleDbDataAdapter da;
            da = new OleDbDataAdapter(strSQL, cn);
            da.Fill(dt);
            // B5: Dong ket noi CSDL
            cn.Close();
            return dt;
        }

        public bool isNumberic(string str)
        {
            try
            {
                str = str.Trim();
                int foo = int.Parse(str);
                return (true);
            }
            catch (FormatException)
            {
                return (false);
            }
        }
        //-----Ket thuc Insert------
        //-----Bat dau Update------
        public static DataTable LoadDichVu()
        {
            string sql = "select * from DICHVU";
            DataProvider dtb = new DataProvider();
            return dtb.executeQuery(sql);
        }
        //-----Ket thuc Updata------
    }
}
